from sqlalchemy import Column, Integer, String, Text, TIMESTAMP, ForeignKey
from sqlalchemy.sql import func
from sqlalchemy.orm import relationship
from app.db.base import Base


class History(Base):
    __tablename__ = "history"

    id = Column(Integer, primary_key=True, index=True)
    user_id = Column(Integer, ForeignKey("users.id", ondelete="CASCADE"))
    session_id = Column(String(50), ForeignKey("sessions.id", ondelete="CASCADE"))  # 字符型会话ID
    model_name = Column(String(100), nullable=True)  # ✅ 新增字段：用于保存使用的模型名称
    summary = Column(Text)
    last_message = Column(Text)
    updated_at = Column(TIMESTAMP, server_default=func.now(), onupdate=func.now())

    # 关系定义
    session = relationship("Session", back_populates="history")
    user = relationship("User", back_populates="histories")  # 建议补上用户的反向关系

